package com.originskyseed.platform.common.api.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONObject;

public class HttpUtil {
	public static String doGet(String url) {
		CloseableHttpClient httpClient = null;
		CloseableHttpResponse response = null;
		String result = "";
		try {
			// 通过址默认配置创建一个httpClient实例
			httpClient = HttpClients.createDefault();
			// 创建httpGet远程连接实例
			HttpGet httpGet = new HttpGet(url);
			// 设置配置请求参数
			RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)// 连接主机服务超时时间
					.setConnectionRequestTimeout(35000)// 请求超时时间
					.setSocketTimeout(60000)// 数据读取超时时间
					.build();
			// 为httpGet实例设置配置
			httpGet.setConfig(requestConfig);
			// 执行get请求得到返回对象
			response = httpClient.execute(httpGet);
			// 通过返回对象获取返回数据
			HttpEntity entity = response.getEntity();
			// 通过EntityUtils中的toString方法将结果转换为字符串
			result = EntityUtils.toString(entity);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 关闭资源
			if (null != response) {
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (null != httpClient) {
				try {
					httpClient.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}

	public static String doPost(String url, Map<String, Object> paramMap) {
		CloseableHttpClient httpClient = null;
		CloseableHttpResponse httpResponse = null;
		String result = "";
		// 创建httpClient实例
		httpClient = HttpClients.createDefault();
		// 创建httpPost远程连接实例
		HttpPost httpPost = new HttpPost(url);
		// 配置请求参数实例
		RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)// 设置连接主机服务超时时间
				.setConnectionRequestTimeout(35000)// 设置连接请求超时时间
				.setSocketTimeout(60000)// 设置读取数据连接超时时间
				.build();
		// 为httpPost实例设置配置
		httpPost.setConfig(requestConfig);
		// 设置请求头
		// 封装post请求参数
		if (null != paramMap && paramMap.size() > 0) {
			List<NameValuePair> nvps = new ArrayList<NameValuePair>();
			// 通过map集成entrySet方法获取entity
			Set<Map.Entry<String, Object>> entrySet = paramMap.entrySet();
			// 循环遍历，获取迭代器
			Iterator<Map.Entry<String, Object>> iterator = entrySet.iterator();
			while (iterator.hasNext()) {
				Map.Entry<String, Object> mapEntry = iterator.next();
				nvps.add(new BasicNameValuePair(mapEntry.getKey(), mapEntry.getValue().toString()));
			}

			// 为httpPost设置封装好的请求参数
			try {
				httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		try {
			// httpClient对象执行post请求,并返回响应参数对象
			httpResponse = httpClient.execute(httpPost);
			// int code=httpResponse.getStatusLine().getStatusCode();
			// System.out.println("==code=="+code);
			// 从响应对象中获取响应内容
			HttpEntity entity = httpResponse.getEntity();
			result = EntityUtils.toString(entity);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 关闭资源
			if (null != httpResponse) {
				try {
					httpResponse.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (null != httpClient) {
				try {
					httpClient.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}


	public static String doPost(String url, JSONObject jsonObject) {
		CloseableHttpClient httpClient = null;
		CloseableHttpResponse httpResponse = null;
		String result = "";
		// 创建httpClient实例
		httpClient = HttpClients.createDefault();
		// 创建httpPost远程连接实例
		HttpPost httpPost = new HttpPost(url);
		// 配置请求参数实例
		RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)// 设置连接主机服务超时时间
				.setConnectionRequestTimeout(35000)// 设置连接请求超时时间
				.setSocketTimeout(60000)// 设置读取数据连接超时时间
				.build();
		// 为httpPost实例设置配置
		httpPost.setConfig(requestConfig);
		// 设置请求头
		httpPost.setHeader("Content-Type","application/json");
		httpPost.setEntity(new StringEntity(JSONObject.toJSONString(jsonObject), ContentType.create("application/json","utf-8")));
		try {
			// httpClient对象执行post请求,并返回响应参数对象
			httpResponse = httpClient.execute(httpPost);
			// int code=httpResponse.getStatusLine().getStatusCode();
			// System.out.println("==code=="+code);
			// 从响应对象中获取响应内容
			HttpEntity entity = httpResponse.getEntity();
			result = EntityUtils.toString(entity);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 关闭资源
			if (null != httpResponse) {
				try {
					httpResponse.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (null != httpClient) {
				try {
					httpClient.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}

	public static void main(String[] args) {
		String domainUrl = "http://external-test.jiaoyoushow.com";
		// 家族
		String url1 = "/kingDee/family/info";
		String param = "familyId=200214";
		url1 = url1 + "?" + param;
		String content = doGet(domainUrl + url1);
		// 主播
		url1 = "/kingDee/anchor/info";
		param = "anchorId=110014770";
		url1 = url1 + "?" + param;
		content = doGet(domainUrl + url1);

		// 主播
		url1 = "/kingDee/anchor/info/batch";
		param = "anchorIds=110021157,110020915";
		url1 = url1 + "?" + param;
		content = doGet(domainUrl + url1);

		// 推广公会（暂时没有）
		url1 = "/kingDee/guild/info";
		param = "tradeUnionId=110020915";
		url1 = url1 + "?" + param;

		// 查询产品信息
		url1 = "/kingDee/guild/channel/app/info";
		param = "channelId=M234113_S234114";
		url1 = url1 + "?" + param;
		content = doGet(domainUrl + url1);

		// 查询产品信息
		url1 = "/kingDee/guild/channel/app/info";
		param = "channelId=M0_S0";
		url1 = url1 + "?" + param;
		content = doGet(domainUrl + url1);

		// 根据产品id和子渠道id获取最新版本接口
		url1 = "/kingDee/anchor/channel/promote";
		param = "channelId=M234113_S234114&productId=wasai002&anchorIds=";
		url1 = url1 + "?" + param;
		content = doGet(domainUrl + url1);

		// 根据产品id获取最新版本接口
		url1 = "/kingDee/app/version";
		param = "productId=wasai002";
		url1 = url1 + "?" + param;
		content = doGet(domainUrl + url1);

		// 玩家奖励申请查询接口
		url1 = "/kingDee/user/award/info";
		param = "playId=110020954&channelId=M234113_S234114";
		url1 = url1 + "?" + param;

		content = doGet(domainUrl + url1);
		System.out.println("==content=" + content);

		// 玩家奖励申请查询接口
		url1 = "/kingDee/user/award/submit";

		Map map = new HashMap();
		map.put("playId", "110020954");
		map.put("channelId", "M234113_S234114");
		map.put("awardType", "1");
		map.put("awardRule", "3");
		map.put("awardPrizeId", "C15");
		map.put("sendVip", "true");
		content = doPost(domainUrl + url1, map);

		map = new HashMap();
		map.put("playId", "110020954");
		map.put("channelId", "M234113_S234114");
		map.put("awardType", "3");
		map.put("awardRule", "2");
		map.put("awardPrizeId", "C16");
		map.put("sendVip", "true");
		content = doPost(domainUrl + url1, map);

		// 违规信息
		url1 = "/kingDee/anchor/violation/info";
		param = "?family_id=38&pageNum=1&pageSize=10&anchorId=&beginDate=&endDate=";
		url1 = url1 + "?" + param;

		content = doGet(domainUrl + url1);

		url1 = "/kingDee/anchor/live/setStatus";
		map = new HashMap();
		map.put("anchorId", "110021157");
		map.put("status", 0);
		map.put("type", 53);
		content = doPost(domainUrl + url1, map);

		url1 = "/kingDee/anchor/live/setStatus";
		map = new HashMap();
		map.put("anchorId", "110021157");
		map.put("status", 1);
		map.put("type", 0);
		content = doPost(domainUrl + url1, map);

		url1 = "/kingDee/channel/union/info?channelId=M234113_S234114";
		content = doGet(domainUrl + url1);

		System.out.println("==content==" + content);

		url1 = "/kingDee/user/follow/list?pageNum=1&pageSize=10";
		content = doGet(domainUrl + url1);
		System.out.println("==content==" + content);


		url1 = "/kingDee/user/online?playerIds=110021367,110021349,110020268";
		content = doGet(domainUrl + url1);
		System.out.println("==content==" + content);

		url1 = "/kingDee/anchor/online?anchorIds=110020915,110021806,110021157";
		content = doGet(domainUrl + url1);
		System.out.println("==content==" + content);

		//改绑
		url1 = "/kingDee/user/channel/apply";
		JSONObject jsonObject=new JSONObject();
		jsonObject.put("vvId", "110021157");
		jsonObject.put("newScId", "M234233_S234234");
		jsonObject.put("segmentingLine", "2021-8-17 19:00");
		jsonObject.put("recordType", 1);
		jsonObject.put("opType", 1);
		jsonObject.put("tieUpReason", "推广平台同公会改绑改绑");
		jsonObject.put("groupId","25025");
		jsonObject.put("loginName", "test");
		content = doPost(domainUrl + url1, jsonObject);
		System.out.println("==content==" + content);
	}
}
